package blackberryAppCom;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

public class BBApp_Common {
	
	final static String driverName = "oracle.jdbc.driver.OracleDriver";
	
	//final static String serverName = "192.168.103.140";
	//final static String portNumber = "1521";
	//final static String sid = "testdev";
	//final static String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
	
	final static String url = "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.103.3)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.103.5)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.103.7)(PORT = 1521)))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = app1)))";
	
	//final static String url = "jdbc:oracle:thin:@192.168.103.3:1521:app1";
	//final static String url = "jdbc:oracle:thin:@192.168.103.5:1521:app1";
	//final static String url = "jdbc:oracle:thin:@192.168.103.7:1521:app1";
	
	//final static String url = "jdbc:oracle:thin:@192.168.103.140:1521:testdev";
	
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    ResultSetMetaData rsmd = null;
    
    private String lStrFilePath;
//    private String lStrDC_FilePath; //Directory for Data Conversion logger

    private boolean lBlnLogging;
    //private static final String lStrFACTORY = "com.ibm.ejs.ns.jndi.CNInitialContextFactory";
    
	/**
	 * get screen width
	 */
	public static int fnGetScrWidth() {
		int intWidth = 0;
		Toolkit toolkit =  Toolkit.getDefaultToolkit ();
	    Dimension dim = toolkit.getScreenSize();
    	intWidth = dim.width;
    	return intWidth;
	}
	
	/**
	 * get screen height
	 */
	public static int fnGetScrHeight() {
		int intHeight = 0;
		Toolkit toolkit =  Toolkit.getDefaultToolkit ();
	    Dimension dim = toolkit.getScreenSize();
    	intHeight = dim.height;
		return intHeight;
	}    
    
    /** fnCutSpace */
	public static String fnCutSpace(String strSource) {
		String strResult = "";
		int intSource = strSource.length();
		for (int a=0; a<=intSource-1; a++) {
			char chAtIdx = strSource.charAt(a);
			if ((""+chAtIdx).equals(" ")) {
				strSource = strSource.substring(0, a) + strSource.substring(a+1, strSource.length());
				intSource = intSource - 1;
			}
		}
		strResult = strSource;
		return strResult;
	}
    
    /** system out println */
	public static void fnPrint(String strMsg) {
		String lStrDebug = fnGetProperty("DEBUG_MODE");
		if (lStrDebug.equalsIgnoreCase("Y")) {
			System.out.println("[Debug Mode] Message: " + strMsg);
		}
	}
	
	/** fnGetDateDDMMMYYYY(String) */
	public static String fnGetDateDDMMMYYYY(String fStrSource) {
		//  In Param	: Date in format YYYY-MM-DD
		// 	Remark		: Convert the date in format DD/MM/YYYY for display in JSP
		//	Return		: Date in format DD-MMM-YYYY
			String passValue_value = "";
			
			if(fStrSource != null && fStrSource.length() != 0 && !fStrSource.equals("")) 
			{
				String lStrYYYY		= fStrSource.substring(0,4);
				String lStrMM		= fnGetMonth(fStrSource.substring(5,7));
				String lStrDD		= fStrSource.substring(8,10);
				passValue_value		= lStrDD +"-"+ lStrMM +"-"+ lStrYYYY;
			}
			return passValue_value;
		}
	
    /** fnGetDateDDMMYYYY(String) */
    public static String fnGetDateDDMMYYYY(String fStrSource) {
	//  In Param	: Date in format YYYY-MM-DD
	// 	Remark		: Convert the date in format DD/MM/YYYY for display in JSP
	//	Return		: Date in format DD/MM/YYYY
		String passValue_value = "";
		if(fStrSource != null && fStrSource.length() != 0 && !fStrSource.equals("")) {
			String lStrYYYY		= fStrSource.substring(0,4);
			String lStrMM		= fStrSource.substring(5,7);
			String lStrDD		= fStrSource.substring(8,10);
			passValue_value		= lStrDD +"/"+ lStrMM +"/"+ lStrYYYY;
		}
		return passValue_value;
	}
    
    /** fnGetMonthNumber */
    public static String fnGetMonth(String fStrSource) {
    	//  In Param	: 01,02,03...
    	// 	Remark		: Convert the date in format DD/MMM/YYYY for display in JSP
    	//	Return		: Jan, Feb, Mar...
    		String passValue_value = "";
    		
		if(fStrSource != null && fStrSource.length() != 0 && !fStrSource.equals("")) 
		{
			if (fStrSource.equalsIgnoreCase("01")) {
				passValue_value = "Jan";
			} else if (fStrSource.equalsIgnoreCase("02")) {
				passValue_value = "Feb";
			} else if (fStrSource.equalsIgnoreCase("03")) {
				passValue_value = "Mar";
			} else if (fStrSource.equalsIgnoreCase("04")) {
				passValue_value = "Apr";
			} else if (fStrSource.equalsIgnoreCase("05")) {
				passValue_value = "May";
			} else if (fStrSource.equalsIgnoreCase("06")) {
				passValue_value = "Jun";
			} else if (fStrSource.equalsIgnoreCase("07")) {
				passValue_value = "Jul";
			} else if (fStrSource.equalsIgnoreCase("08")) {
				passValue_value = "Aug";
			} else if (fStrSource.equalsIgnoreCase("09")) {
				passValue_value = "Sep";
			} else if (fStrSource.equalsIgnoreCase("10")) {
				passValue_value = "Oct";
			} else if (fStrSource.equalsIgnoreCase("11")) {
				passValue_value = "Nov";
			} else if (fStrSource.equalsIgnoreCase("12")) {
				passValue_value = "Des";
			} else {
				passValue_value = "";
			}
		}
		return passValue_value;
	}
    	
	/** fnGetMonthNumber */
	public static String fnGetMonthNumber(String fStrSource) {
    	//  In Param	: Jan, Feb, Mar...
    	// 	Remark		: Convert the date in format DD/MMM/YYYY for display in JSP
    	//	Return		: 01, 02, 03...
		String passValue_value = "";
		
		if(fStrSource != null && fStrSource.length() != 0 && !fStrSource.equals("")) 
		{
			if (fStrSource.equalsIgnoreCase("Jan")) {
				passValue_value = "01";
			} else if (fStrSource.equalsIgnoreCase("Feb")) {
				passValue_value = "02";
			} else if (fStrSource.equalsIgnoreCase("Mar")) {
				passValue_value = "03";
			} else if (fStrSource.equalsIgnoreCase("Apr")) {
				passValue_value = "04";
			} else if (fStrSource.equalsIgnoreCase("May")) {
				passValue_value = "05";
			} else if (fStrSource.equalsIgnoreCase("Jun")) {
				passValue_value = "06";
			} else if (fStrSource.equalsIgnoreCase("Jul")) {
				passValue_value = "07";
			} else if (fStrSource.equalsIgnoreCase("Aug")) {
				passValue_value = "08";
			} else if (fStrSource.equalsIgnoreCase("Sep")) {
				passValue_value = "09";
			} else if (fStrSource.equalsIgnoreCase("Oct")) {
				passValue_value = "10";
			} else if (fStrSource.equalsIgnoreCase("Nov")) {
				passValue_value = "11";
			} else if (fStrSource.equalsIgnoreCase("Dec")) {
				passValue_value = "12";
			} else {
				passValue_value = "";
			}
		}
		return passValue_value;
	}
    
    /** fnChkInteger(String) */
    public static int fnChkInteger(String fStrSource) throws NumberFormatException {
	    int lIntSource = 0;
	    fStrSource = fnGetValue(fStrSource);
	    if(fStrSource != null && fStrSource.length() != 0)
	        try {
	            lIntSource = Integer.parseInt(fnCutComma(fStrSource));
	        }
	        catch(NumberFormatException _ex) {
	            throw new NumberFormatException();
	        }
	        catch(Exception _ex) { 
	        	// nothing
	        }
	    return lIntSource;
	}
    
    /** fnGetPropertyMessage(String, String) */
    public static String fnGetPropertyMessage(String lStrLangCode, String lStrProp) {
    	try {
       		PropertyResourceBundle configBundle = (PropertyResourceBundle)ResourceBundle.getBundle("ApplicationResources_"+lStrLangCode.trim().toLowerCase());
        	return configBundle.getString(lStrProp);
    	} catch(Exception e) {
    		    fnWriteLog("UTIL_LOGGER", "UTIL_LOGGER(fnGetPropertyMessage) : ","Error : "+e.getMessage());
    	        return lStrProp;
    	}	
    }
    
    /** fnFormatNum(String, String) */
    public static String fnFormatNum(String fStrSource,String fStrLang)
	{
		String lStrReturn="";
		int lIntDec = fnChkInteger(fnGetPropertyMessage(fStrLang,"NUM_DEC"));
		String lStrThdDel = fnGetPropertyMessage(fStrLang,"NUM_THD_DEL");
		char lCharThdDel = lStrThdDel.charAt(0);
		String lStrDecDel = fnGetPropertyMessage(fStrLang,"NUM_DEC_DEL");
		char lCharDecDel = lStrDecDel.charAt(0);
		
		String lStrNefDel = fnGetPropertyMessage(fStrLang,"NUM_NEF_DEL");
		String lStrNebDel = fnGetPropertyMessage(fStrLang,"NUM_NEB_DEL");
		String lStrPofDel = fnGetPropertyMessage(fStrLang,"NUM_POF_DEL");
		String lStrPobDel = fnGetPropertyMessage(fStrLang,"NUM_POB_DEL");
		
//System.err.println("fStrSource********"+fStrSource);		
		if(fStrSource==null || fStrSource.equals(""))
		{
			lStrReturn = "0"+lCharDecDel;
			for(int i=0;i<lIntDec;i++)
			{
				lStrReturn = lStrReturn+"0";
			}
			lStrReturn = lStrPofDel+lStrReturn+lStrPobDel;
		}
		else
		{
			
			String sign="";
			String cents="";
			if(fStrSource.indexOf("-")==0)
			{
				sign=fStrSource.substring(0,1);
				fStrSource=fStrSource.substring(1,fStrSource.length());
			}
			if(fStrSource.indexOf("E")>-1)
			{
				int lIntPwr = Integer.parseInt(fStrSource.substring(fStrSource.indexOf("E")+1,fStrSource.length()));
				String lStrTemp = fStrSource.substring(fStrSource.indexOf(".")+1,fStrSource.indexOf("E"));
				int lIntTemp = lStrTemp.length();
				if(lStrTemp.length()<lIntPwr)
				{
					for(int i=0;i<lIntPwr-lIntTemp;i++)
					{
						lStrTemp = lStrTemp+"0";
					}
				}
				String lStrTemp1 = "";
				for(int i=0;i<lStrTemp.length();i++)
				{
					if(i==lIntPwr)
					{
						lStrTemp1 = lStrTemp1+"."+lStrTemp.substring(i,i+1);
					}
					else
					{
						lStrTemp1 = lStrTemp1+lStrTemp.substring(i,i+1);
					}
				}
				lStrTemp = fStrSource.substring(0,fStrSource.indexOf("."))+lStrTemp1;
				fStrSource = lStrTemp;
			}
			if(fStrSource.indexOf(".")>-1)
			{
				cents = fStrSource.substring(fStrSource.indexOf(".")+1,fStrSource.length());
				fStrSource = fStrSource.substring(0,fStrSource.indexOf("."));
//System.err.println("fStrSource---------------"+fStrSource);
				
				if (fStrSource.trim().equals("")) {
					fStrSource = "0";
				}	
			}
			
			if(cents=="")
			{
				for(int i=0;i<lIntDec;i++)
				{
					cents=cents+"0";
				}
			}
			else
			{
				if(cents.length()<lIntDec)
				{
					for(int i=0;i<lIntDec-cents.length();i++)
					{
						cents=cents+"0";
					}
				}
				else if(cents.length()>lIntDec)
				{
					cents=cents.substring(0,lIntDec);
				}
			}
			
			for(int i=0; i<Math.floor((fStrSource.length()-(1+i))/3); i++)
  				fStrSource = fStrSource.substring(0, fStrSource.length()-(4*i+3))+ lCharThdDel +fStrSource.substring(fStrSource.length()-(4*i+3));
  				
  				
  			
  			String suf="";
  			if(sign.trim().equals("-"))
  			{
  				sign=lStrNefDel;
  				suf=lStrNebDel;
  			}
  			else
  			{
  				sign=lStrPofDel;
  				suf=lStrPobDel;
  			}
  			
			if (fStrSource.trim().equals("") && !cents.trim().equals("")) {
				fStrSource = "0";
			}	
			
			lStrReturn = sign+fStrSource+lCharDecDel+cents+suf;
//System.err.println("cents********"+cents);			
//System.err.println("fStrSource********"+fStrSource);			
//System.err.println("suf********"+suf);			
//System.err.println("lCharDecDel********"+lCharDecDel);
			
//System.err.println("lStrReturn********"+lStrReturn);	

				
		}
		return lStrReturn;
	}
    
    /** fnCut(String, char) */
    private static String fnCut(String fStrSource, char cutChar) {
        String fStrReturn = "";
        for (int i=0; i<fStrSource.length();i++) {                       
           if (fStrSource.charAt(i) != cutChar ) {
              fStrReturn = fStrReturn + fStrSource.charAt(i);
           }
        }
        return fStrReturn;
    }
    
    /** fnCutComma(String) */
    public static String fnCutComma(String fStrSource) {
    	return fnCut(fStrSource,',');
    }
    
    /** fnFormatNumber Indonesia */
    public static String fnFormatNumberInd(String fStrSource) {
		String sign = "";
		String passValue_value = "";
		
		if(fStrSource != null && fStrSource.length() != 0 && !fStrSource.equals("")) {	
			fStrSource = fnCutComma(fStrSource);
			
			int lIntRecCount = fStrSource.indexOf(".");
			if(lIntRecCount != -1) 
				fStrSource = fStrSource.substring(0,lIntRecCount);

			if (fStrSource.substring(0,1).equals("-")){
				sign = fStrSource.substring(0,1);
				fStrSource = fStrSource.substring(1);
			}
			
			for(int i=0; i<((fStrSource.length()-(1+i))/3); i++) {
				fStrSource = fStrSource.substring(0, fStrSource.length()-(4*i+3))+ "."+fStrSource.substring(fStrSource.length()-(4*i+3));
			}
			
			passValue_value = sign + fStrSource;
		} else {
			passValue_value = "0";
		}
			
		return 	passValue_value + ",00";
	}
    
    /** fnGetDate() */
    private static String fnGetDate() {
        try {
 			StringBuffer lSbrDate	= new StringBuffer();
 			  
        	int lIntDay		= 0;
        	int lIntMonth	= 0;
        	int lIntYear	= 0;
			
			Calendar  lMisCalc	= java.util.Calendar.getInstance();
			lIntDay		= lMisCalc.get(Calendar.DATE);
			lIntYear	= lMisCalc.get(Calendar.YEAR);
        	lIntMonth	= lMisCalc.get(Calendar.MONTH)+1;        	
            
            lSbrDate.append(lIntYear);
            if(lIntMonth<10)	lSbrDate.append("0").append(lIntMonth);
            else 				lSbrDate.append(lIntMonth);
            if(lIntDay<10)	lSbrDate.append("0").append(lIntDay);
            else 				lSbrDate.append(lIntDay);
            lSbrDate.append(".log");
            return lSbrDate.toString();
        }
        catch(Exception e) {
            fnWriteLog("IIMS_LOG", "UTIL_LOGGER", "Error while getting the filename for logging : " + e.getMessage());
        }
        return null;
    }
    
    /** fnSetProperties(Log) */
    protected static void fnSetProperties(BBApp_Common lObjLog) {
   		FileInputStream is = null;
        try {
        	is = new FileInputStream("/usr/bbprop/SimasInfo2.properties");
    		Properties prop = new Properties();
    		prop.load(is);
    		// Retrieve properties
    		lObjLog.lStrFilePath = prop.getProperty("DIRECTORY");
//            lObjLog.lStrDC_FilePath = prop.getProperty("DC_DIRECTORY");
          	String lStrLog = prop.getProperty("LOGGING");

            //PropertyResourceBundle configBundle = (PropertyResourceBundle)ResourceBundle.getBundle("IIMS");
            //lObjLog.lStrFilePath = configBundle.getString("DIRECTORY");
            //lObjLog.lStrDC_FilePath = configBundle.getString("DC_DIRECTORY");
          	//String lStrLog = configBundle.getString("LOGGING");
          	
            if(lObjLog.lStrFilePath == null || lStrLog == null) {
                lObjLog.lBlnLogging = false;
            } else {                
            	lStrLog	= lStrLog.trim();
                if(lStrLog.equalsIgnoreCase("TRUE")) {                
                    lObjLog.lBlnLogging = true;				
                } else {
                    lObjLog.lBlnLogging = false;
                }
            }
        }
        catch(Exception e) {
           // e.printStackTrace();
			BBApp_Common.fnWriteLog("BBApp_Common", "BBApp_Common(fnSetProperties) : ","Exception: "+e);
//            System.err.println("BBApp_Common Error : " + e.getMessage());
        }
        finally {
			try {
				if(is!=null) { is.close(); is=null;}
			}
			catch(Exception e) {
				BBApp_Common.fnWriteLog("BBApp_Common", "BBApp_Common(fnSetProperties) : ","Close Exception: "+e);
			}
		}
    }
    
    /** fnWriteLog(String, String, String) */
    public static void fnWriteLog(String lStrLogName, String lStrClassName, String lStrMess)
    {
    	FileWriter lMisLog = null;
        try {
            BBApp_Common lObjLog = new BBApp_Common();
            fnSetProperties(lObjLog);
            if(!lObjLog.lBlnLogging)
                return;
                
            //String lStrFileName = lStrLogName + fnGetDate();
            String lStrFileName = "BBLOG_"+fnGetDate();
            if(lStrFileName == null) {
//                System.err.println("Error in getting log file name");
                return;
            }
            lMisLog = new FileWriter(lObjLog.lStrFilePath + "/" + lStrFileName, true);
            
            String lStrTimeStamp = (new Date()).toString();
            String lStrMessage = "[" + lStrTimeStamp + "]   " + lStrClassName + "  : " + lStrMess; //uncommented by eztan - 23/07/2006
            //String lStrMessage = lStrClassName + "  : " + lStrMess;
            char buffer[] = new char[lStrMessage.length()];
            lStrMessage.getChars(0, lStrMessage.length(), buffer, 0);
            lMisLog.write(buffer);
            lMisLog.write("\r\n");
          //  lMisLog.close();
        }
        catch(Exception e) {
           // e.printStackTrace();
//            System.err.println("BBApp_Common Error : " + e.getMessage());
				BBApp_Common.fnWriteLog("BBApp_Common", "BBApp_Common(fnWriteLog) : ","Exception: "+e);
        }
        finally {
			try {
				if(lMisLog!=null) { lMisLog.close(); lMisLog=null;}
			}
			catch(Exception e) {
				BBApp_Common.fnWriteLog("BBApp_Common", "BBApp_Common(fnWriteLog) : ","Close Exception: "+e);
			}
		}
    }    
    
    /** fnGetProperty(String) */
    public static String fnGetProperty(String lStrProp) {
		FileInputStream is = null;
		Properties prop = new Properties();
		try {
			// untuk akses dgn simulator
			is = new FileInputStream("/usr/bbprop/SimasInfo2.properties");
			
			// untuk akses dgn blackberry
			//is = new FileInputStream("/usr/bbprop/SimasLive.properties");
			
    		prop.load(is);			
		} catch (FileNotFoundException e) {
			// e.printStackTrace();
			BBApp_Common.fnWriteLog("BBApp_Common", "BBApp_Common(fnGetProperty) : ","Exception: "+e);
		} catch (IOException e) {
//			 e.printStackTrace();
			BBApp_Common.fnWriteLog("BBApp_Common", "BBApp_Common(fnGetProperty) : ","Exception: "+e);
		}
		finally {
			try {
				if(is!=null) { is.close(); is=null;}
			}
			catch(Exception e) {
				BBApp_Common.fnWriteLog("BBApp_Common", "BBApp_Common(fnGetProperty) : ","Close Exception: "+e);
			}
		}
        return prop.getProperty(lStrProp);
    }
    
    /** fnReplaceBackSlash(String) */
    public static String fnReplaceBackSlash(String fStrSource) {
		//  In Param	: Input String (to be formatted)
		// 	Remark		: Replace a BackSlash with its ascii code(092)
		//	Return		: String after it is formatted
        StringBuffer lSbrTemp = new StringBuffer("");
        if(fStrSource != null && fStrSource.length() > 0) {
            int lIntIndex = 0;
            for(; fStrSource.indexOf("\\") >= 0; fStrSource = fStrSource.substring(lIntIndex, fStrSource.length())) {
                lIntIndex = fStrSource.indexOf("\\");
                lSbrTemp.append(fStrSource.substring(0, lIntIndex)).append("&#092;");
                lIntIndex++;
            }

            lSbrTemp.append(fStrSource);
        }
        return lSbrTemp.toString();
    }
    
    /** fnReplaceDoubleQuotes(String) */
    public static String fnReplaceDoubleQuotes(String fStrSource) {
		//  In Param	: Input String (to be formatted)
		// 	Remark		: Replace a DoubleQuote with its ascii code(034)
		//	Return		: String after it is formatted
        StringBuffer lSbrTemp = new StringBuffer("");
        if(fStrSource != null && fStrSource.length() > 0) {
            int lIntIndex = 0;
            for(; fStrSource.indexOf("\"") >= 0; fStrSource = fStrSource.substring(lIntIndex, fStrSource.length())) {
                lIntIndex = fStrSource.indexOf("\"");
                lSbrTemp.append(fStrSource.substring(0, lIntIndex)).append("&#034;");
                lIntIndex++;
            }

            lSbrTemp.append(fStrSource);
        }
        return lSbrTemp.toString();
    }
    
    /** fnReplaceQuotes(String) */
    public static String fnReplaceQuotes(String fStrSource) {
        StringBuffer lSbrTemp = new StringBuffer("");
        if(fStrSource != null && fStrSource.length() > 0) {
            int lIntIndex = 0;
            for(; fStrSource.indexOf("'") >= 0; fStrSource = fStrSource.substring(lIntIndex, fStrSource.length())) {
                lIntIndex = fStrSource.indexOf("'");
                lSbrTemp.append(fStrSource.substring(0, lIntIndex)).append("&#039;");
                
                lIntIndex++;
            }

            lSbrTemp.append(fStrSource);
        }
        return lSbrTemp.toString();
    }
    
    /** fnGetValue(String) */
    public static String fnGetValue(String fStrSource) {
        if(fStrSource != null && fStrSource.trim().length() != 0)
        {
        	String lStrValue 	= fnReplaceQuotes(fStrSource).trim();
        	lStrValue 			= fnReplaceDoubleQuotes(lStrValue).trim();
        	lStrValue 			= fnReplaceBackSlash(lStrValue).trim();       	
            return lStrValue;
        }
        else
            return "";
    }
    
	/** Close Connection to Database */
    public void CloseConnection() {
            try {
            	if (rs != null) {
            		rs.close();
            		rs = null;
            	}
            	if (stmt != null) {
            		stmt.close();
            		stmt = null;
            	}
            	if (con != null) {
            		con.close();
            		con = null;
            	}
            }
            catch (Exception ex) {
                    System.out.println("Exception: " + ex);
            }
    }
    
    /** Open Connetion to Database */
    public Connection OpenConnection(int intIdx) {
    	String USERname = "";
        String PASSword = "";
        
    		if (intIdx == 1) {
    			// db2inst2
    			USERname = "db2inst2";
    			PASSword = "password";
    		} else if (intIdx == 2) {
    			// mbu
    			USERname = "mbu";
    			PASSword = "asmmbuapp05it";
    		} else if (intIdx == 3) {
    			// mbu
    			USERname = "berita";
    			PASSword = "berita";
    		}
    		
    		for (int a=1; a<=20; a++) {
    		
	            try {
	            	BBApp_Common.fnWriteLog("BBApp_Common","OpenConnection: "," " + a);
	                Class.forName (driverName);
	                con = DriverManager.getConnection(url, USERname, PASSword);
	                //con.setAutoCommit(true);
	                if (con != null) {
	                	break;
	                }
	            }
	            catch (Exception ex) {
	                    System.out.println("Exception: " + ex + "<br>");
	            }
            
    		}
    		return con;
    }
}
